import _extends from 'babel-runtime/helpers/extends';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
import React, { PureComponent } from 'react';
import Loading from '../loading';

export default (function (loader, componentProps) {
  var _class, _temp2;

  return _temp2 = _class = function (_PureComponent) {
    _inherits(AsyncComponent, _PureComponent);

    function AsyncComponent() {
      var _temp, _this, _ret;

      _classCallCheck(this, AsyncComponent);

      for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
        args[_key] = arguments[_key];
      }

      return _ret = (_temp = (_this = _possibleConstructorReturn(this, _PureComponent.call.apply(_PureComponent, [this].concat(args))), _this), _this.state = {
        Component: AsyncComponent.Component
      }, _temp), _possibleConstructorReturn(_this, _ret);
    }

    AsyncComponent.prototype.componentWillMount = function componentWillMount() {
      var _this2 = this;

      if (!this.state.Component) {
        loader().then(function (Component) {
          AsyncComponent.Component = Component;

          _this2.setState(function () {
            return {
              Component: Component
            };
          });
        });
      }
    };

    AsyncComponent.prototype.componentWillUnmount = function componentWillUnmount() {
      this.loadingInstance && Loading.hide();
    };

    AsyncComponent.prototype.render = function render() {
      var Component = this.state.Component;


      if (Component) {
        if (this.loadingInstance) {
          // Loading.hide()
          this.loadingInstance = null;
        }

        return React.createElement(Component, _extends({}, this.props, componentProps));
      }

      if (!this.loadingInstance) {
        // this.loadingInstance = Loading.show()
      }

      return null;
    };

    return AsyncComponent;
  }(PureComponent), _class.Component = null, _temp2;
});